/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Editor.java
 *
 * Created on Sep 7, 2010, 3:40:12 PM
 */
package Relationship.Complex.Creation;

import Relationship.Complex.DataTypes.ComplexRel;
import java.util.Vector;
import javax.swing.JList;

/**
 * This Class will be used to Edit the Complex Relationships within the current schema.
 * A Complex Relationship in the Schema consists of a name and a Regular expression that
 * defines the Classes that it accepts.
 * <pre>
 * For Example:
 *      Name: Test_Revealed, Regex: (Test)(Problem)+
 *
 *      This Complex Relationship will allow users to associate one Test class annotation
 *      with one or more Problem class annotations.
 * <pre>
 * This GUI will display all Complex Relationships in the schema and provide tools
 * for modifying the schema.
 *
 * @see Relationsihp.Complex.DataTypes.ComplexRel
 * @see Relationship.Complex.DataTypes.RelSchemaList
 * @author Kyle
 */
public class ComplexSchemaManager extends javax.swing.JFrame
{
    //<editor-fold defaultstate="collapsed" desc="Constructors">
    /**
     * Constructor
     * @param caller - the UserInterface.GUI that is creating this editor.
     */
    public ComplexSchemaManager(UserInterface.GUI caller)
    {
        //Initialize Components... DO NOT USE COMPONENTS BEFORE THIS LINE!!!!
        initComponents();
        
        //Set the size of this GUI
        this.setSize(400, 500);
        
        //Initialize Relationship List data
        updateRelationships();
        
        //Keep Track of GUI that created this gui
        gui = caller;

        //Center dialog in caller dialog
        setDialogLocation();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Public Methods">
    /**
     * Update the list of relationship names
     */
    public void updateRelationships()
    {
        Vector<ComplexRel> listInfo = new Vector<ComplexRel>();
        for (ComplexRel att : env.Parameters.ComplexRelationshipNames.getRelationships())
        {
            listInfo.add(att);
        }
        relationshipList.setListData(listInfo);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Private Methods">
    /**
     * Remove the currently selected ComplexRelationship from the schema, and update
     * the relationshipList.
     *
     * @param list - the list to remove values from.
     */
    private void removeSelectedFromSchema(JList list)
    {
        Object[] att = list.getSelectedValues();
        if (att == null || att.length == 0)
        {
            return;
        }
        for (Object obj : att)
        {
            env.Parameters.ComplexRelationshipNames.remove((ComplexRel) obj);
        }
        list.setListData(new Vector());
        updateRelationships();
    }
    /**
     * Place this dialog in the center of the Caller Dialog.
     */
    private void setDialogLocation()
    {
        //If null was passed into the constructor then just return
        if (gui == null)
        {
            return;
        }

        //If non null gui then center the gui in the Callers window.
        int parentX = gui.getX(), parentY = gui.getY();
        int parentWidth = gui.getWidth(), parentHeight = gui.getHeight();
        int width = this.getWidth(), height = this.getHeight();

        int x = parentX + (int) ((parentWidth - width) / 2);
        int y = parentY + (int) ((parentHeight - height) / 2);

        this.setLocation(x, y);
    }
    //</editor-fold>
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        removeName = new javax.swing.JButton();
        edit = new javax.swing.JButton();
        addName = new javax.swing.JButton();
        jPanel13 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        relationshipList = new javax.swing.JList();
        jPanel6 = new javax.swing.JPanel();
        exit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Relationship Schema Editor");
        setMinimumSize(new java.awt.Dimension(500, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(1, 2, 10, 10));

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel9.setLayout(new java.awt.BorderLayout());

        jPanel10.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel10.setLayout(new java.awt.GridLayout(1, 0));

        jPanel11.setLayout(new java.awt.GridLayout(1, 0));

        removeName.setFont(new java.awt.Font("Calibri", 0, 13));
        removeName.setText("Remove Selected");
        removeName.setToolTipText("<html>Remove the relationship that is selected in the list. All to/from information<br> associated with the relationship will also be deleted.</html>");
        removeName.setEnabled(false);
        removeName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeNameActionPerformed(evt);
            }
        });
        jPanel11.add(removeName);

        edit.setText("Edit");
        edit.setEnabled(false);
        edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editActionPerformed(evt);
            }
        });
        jPanel11.add(edit);

        addName.setFont(new java.awt.Font("Calibri", 0, 13));
        addName.setText("Add");
        addName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNameActionPerformed(evt);
            }
        });
        jPanel11.add(addName);

        jPanel10.add(jPanel11);

        jPanel9.add(jPanel10, java.awt.BorderLayout.PAGE_END);

        jPanel13.setLayout(new java.awt.BorderLayout());

        jPanel14.setBackground(new java.awt.Color(102, 153, 255));
        jPanel14.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Calibri", 1, 13));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Relationship");
        jPanel14.add(jLabel2, java.awt.BorderLayout.CENTER);

        jPanel13.add(jPanel14, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        relationshipList.setFont(new java.awt.Font("Calibri", 0, 13));
        relationshipList.setToolTipText("<html>The list of relationships that are allowed within this schema.</html>");
        relationshipList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                relationshipListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(relationshipList);

        jPanel13.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel9.add(jPanel13, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel6.setLayout(new java.awt.BorderLayout());

        exit.setText("Done");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        jPanel6.add(exit, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel6, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //<editor-fold defaultstate="collapsed" desc="Event Handlers">
    /**
     * Call this whenever the currently selected attribute is changed.  If an
     * attribute name is selected enable deletion, and enable addition of Attribute
     * values.  If nothing is selected then disable this actions.
     * @param evt
     */
    private void relationshipListValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_relationshipListValueChanged
    {//GEN-HEADEREND:event_relationshipListValueChanged
        Object[] values = relationshipList.getSelectedValues();
        //If only one Relationship is selected then it can be edited and removed
        if (values.length == 1)
        {
            removeName.setEnabled(true);
            edit.setEnabled(true);
        }
        //If more than one Relationship is selected.. they can be mass removed but not mass edited
        else if (values.length > 1)
        {
            removeName.setEnabled(true);
            edit.setEnabled(false);
        }
        //If no Relationships are selected then disable deleting/editing
        else if (values.length == 0)
        {
            //Disable deletion of Attributes
            removeName.setEnabled(false);
            edit.setEnabled(false);
        }

    }//GEN-LAST:event_relationshipListValueChanged
    /**
     * When the 'Add' button is pressed pull up new Editor to create a new
     * Relationship.
     * @param evt - the event that caused this function to be called.
     */
    private void addNameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addNameActionPerformed
    {//GEN-HEADEREND:event_addNameActionPerformed
        RegexEditor editor = new RegexEditor(null, this);
        editor.setVisible(true);
    }//GEN-LAST:event_addNameActionPerformed

    /**
     * When the remove button under the Attribute list is pressed remove the currently
     * selected attribute name.
     * @param evt
     */
    private void removeNameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeNameActionPerformed
    {//GEN-HEADEREND:event_removeNameActionPerformed
        removeSelectedFromSchema(relationshipList);
    }//GEN-LAST:event_removeNameActionPerformed

    /**
     * Update the complex choices in the main GUI when this GUI closes
     * TODO: See if this is still necessary with refactored Relationship building.
     * @param evt
     */
    private void formWindowClosed(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosed
    {//GEN-HEADEREND:event_formWindowClosed
       
    }//GEN-LAST:event_formWindowClosed

    /**
     * Close the gui when the 'Done'/exit button is pressed.
     * @param evt
     */
    private void exitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitActionPerformed
    {//GEN-HEADEREND:event_exitActionPerformed
        this.dispose();
    }//GEN-LAST:event_exitActionPerformed

    /**
     * Open up a new Complex Relationship Editor with the selected Relationship's
     * info already filled in.  Users can then modify existing relationships
     * @param evt
     */
    private void editActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editActionPerformed
    {//GEN-HEADEREND:event_editActionPerformed
        ComplexRel rel = (ComplexRel) relationshipList.getSelectedValue();
        removeSelectedFromSchema(relationshipList);
        RegexEditor editor = new RegexEditor(rel, this);
        editor.setVisible(true);
    }//GEN-LAST:event_editActionPerformed
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Member Variables">
    UserInterface.GUI gui;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addName;
    private javax.swing.JButton edit;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList relationshipList;
    private javax.swing.JButton removeName;
    // End of variables declaration//GEN-END:variables
    //</editor-fold>
}
